package com.xjc.business.controller.sysadmin;

import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.sysadmin.SchoolDetailVo;
import com.xjc.business.entity.vo.sysadmin.SchoolPageVo;
import com.xjc.business.entity.vo.sysadmin.SchoolVo;
import com.xjc.business.service.SchoolsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 徐锦程
 * 时间:2025-01-25 19:45
 */
@RestController("sys_school_controller")
@RequestMapping("/sys_admin/school")
public class SchoolController {

    @Autowired
    SchoolsService schoolsService;

    /**
     * 分页查询学校
     * @param pageSize 每页条数
     * @param currentPage 当前页码
     * @param schoolName 学校名称
     * @return 学校数据
     */
    @GetMapping("/page/{pageSize}/{currentPage}")
    public Result<PageVo<SchoolPageVo>> getSchoolPage(@PathVariable(value = "pageSize", required = false) Integer pageSize,@PathVariable(value = "currentPage",required = false) Integer currentPage, String schoolName){
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }
        if(schoolName == null){
            schoolName = "";
        }
        return schoolsService.getSchoolPage(pageSize,currentPage,schoolName);
    }

    /**
     * 获取学校详情
     * @param id 学校id
     * @return 详情
     */
    @GetMapping("/detail/{id}")
    public Result<SchoolDetailVo> getSchoolDetail(@PathVariable(value = "id",required = false) Integer id){
        if(id == null || id <= 0){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        return schoolsService.getSchoolDetail(id);
    }


    /**
     * 获取学校列表
     * @return 学校列表
     */
    @GetMapping("/list")
    public Result<List<SchoolVo>> getSchoolList(){
        return schoolsService.getSchoolList();
    }
}
